package com.linck.im.client.controller;

import cn.hutool.crypto.SecureUtil;
import com.linck.im.client.context.AccountContext;
import com.linck.im.client.context.ChannelContext;
import com.linck.im.client.context.ControllerContext;
import com.linck.im.model.proto.ProtoMsg;
import io.netty.channel.Channel;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LoginController {

    private static final Logger log = LoggerFactory.getLogger(LoginController.class);

    public LoginController() {
        ControllerContext.putController(this.getClass(), this);
    }

    @FXML
    private Label welcomeText;

    @FXML
    private Label netInfoText;

    @FXML
    private TextField usernameField;

    @FXML
    private PasswordField passwordField;

    @FXML
    private Button loginButton;

    @FXML
    protected void onKeyPressed1(KeyEvent keyEvent) throws InterruptedException {
        if (keyEvent.getCode().equals(KeyCode.ENTER)) {
            onLoginButtonClick();
        }
    }

    @FXML
    protected void onLoginButtonClick() throws InterruptedException {
        welcomeText.setText("logging...");
        String username = usernameField.getText();
        String password = passwordField.getText();
        welcomeText.setText("Welcome to IM, " + username + ":" + password);

        Channel channel = ChannelContext.getChannel();

        ProtoMsg.LoginInfo loginInfo = ProtoMsg.LoginInfo.newBuilder()
                .setAccount(ProtoMsg.Account.newBuilder().setAccount(username).setPassword(SecureUtil.sha1(password)).build())
                .build();
        channel.writeAndFlush(loginInfo);
        log.info("登录中");
        while (true) {
            if (AccountContext.LOGIN_STATUS == -1) {
                return;
            } else if (AccountContext.LOGIN_STATUS == 1) {
                AccountContext.account = username;
                break;
            }
            Thread.sleep(500);
        }
        welcomeText.setText("登录成功");
    }

    public static void drawNetworkInfo() {
        LoginController loginController = (LoginController) ControllerContext.getController(LoginController.class);
        Platform.runLater(() -> {
            loginController.instanceDrawNetworkInfo();
        });
    }

    public void instanceDrawNetworkInfo() {
        if (ChannelContext.NET_STATE == 1) {
            netInfoText.setText("连接服务器成功");
        } else {
            netInfoText.setText("连接服务器失败");
            loginButton.setDisable(true);
        }
    }

    public static void drawWelcomeText(String text) {
        LoginController loginController = (LoginController) ControllerContext.getController(LoginController.class);
        Platform.runLater(() -> {
            loginController.instanceDrawWelcomeText(text);
        });
    }

    public void instanceDrawWelcomeText(String text) {
        welcomeText.setText(text);
    }

}